package com.vanke.kotlin

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.Toolbar
import android.util.Log
import com.vanke.kotlin.domain.RequestFoecastCommand
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.find
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.uiThread

class MainActivity : AppCompatActivity(),ToolbarManager{

    private val items = listOf<String>(
            "Mon 6/23 - Sunny - 31/17",
            "Tue 6/24 - foggy - 21/8",
            "wed 6/25 - cloudy - 22/17",
            "thurs 6/26 - rainy - 18/11",
            "fri 6/27 - foggy - 21/10",
            "sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
            "SUN 6/29 - sunny - 20/7"
    )

    val zipCode: Long by DelegateExt.prefrence(this,SettingActivity.ZIP_CODE,SettingActivity.DEFAULT_ZIP)

    override val toolbar by lazy { find<Toolbar>(R.id.toolbar) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initToolbar()
        recyclerview.layoutManager = LinearLayoutManager(this)
        attachToScroll(recyclerview)

    }


    override fun onResume() {
        super.onResume()
        load()
        Log.d("tag",zipCode.toString())
    }
    private fun load() =
        doAsync {
            val result = RequestFoecastCommand(zipCode).execute()
            uiThread {
                val adapter = ForecastListAdapter(result) {
                    startActivity<DetailActivity>(DetailActivity.ID to it.id,
                            DetailActivity.CITY_NAME to result.city)
                }

                recyclerview.adapter = adapter
                toolbarTitile = "${result.city} (${result.country})"
            }
        }
}


